package net.sourceforge.peers.gui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * 拨号键盘
 * @author Mr.Yan
 */
public class Keypad extends JPanel implements ActionListener {

    private static final long serialVersionUID = 1L;

    public static final String CHARS = "123456789*0#";

    private final CallFrame callFrame;

    public Keypad(CallFrame callFrame) {
        this.callFrame = callFrame;
        initComponents();
    }

    private void initComponents() {
        setLayout(new GridLayout(4, 3, 10, 5));
        for (int i = 0; i < CHARS.length(); ++i) {
            char[] c = { CHARS.charAt(i) };
            String digit = new String(c);
            JButton button = new JButton(digit);
            button.setActionCommand(digit);
            button.addActionListener(this);
            add(button);
        }
        setBorder(BorderFactory.createEmptyBorder(5, 10, 0, 10));
        Dimension dimension = new Dimension(180, 115);
        setMinimumSize(dimension);
        setMaximumSize(dimension);
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        String command = actionEvent.getActionCommand();
        callFrame.keypadEvent(command.charAt(0));
    }

}
